package demo2;

public class MyLinkedList2 {
    static class ListNode{
        public int val;
        public ListNode next;//成员变量
        public ListNode prev;//主要是地址问题，所以我们用ListNode作为参数的变量

        public ListNode(int val){
            this.val = val;
        }
    }

    public ListNode head = null;
    public ListNode last = null;

    public void display(){
        ListNode cur = head;
        while(cur != null){
            System.out.print(cur.val + " ");
            cur = cur.next;
        }
        System.out.println();
    }

    //头插法
    public void addFirst(int data){
        ListNode node = new ListNode(data);
        //假如链表里面没有节点 我们就可以直接插入
        if(head == null){
            head = node;
            last = node;
        } else {
            //链表里面有节点
            node.next = head;
            head.prev = node;
            node.prev = null;
            head = node;
        }
    }

    //尾插法
    public void addLast(int data){
        //ListNode cur = head;
        ListNode node = new ListNode(data);
        if (head == null){
            head = node;
            last = node;
        } else {
            last.next = node;
            node.prev = last;
            last = node;
        }
    }

    //任意位置插入，第一个数据节点为0号下标
    public void addIndex(int index, int data){
        if(index < 0 || index > size()){
            System.out.println("插入的位置不合法");
            return;
        }
        //插第一个节点的位置
        if (index == 0){
            addFirst(data);
            return;
        }
        //插最后位置的节点
        if (index == size()){
            addLast(data);
            return;
        }
        ListNode cur = findIndex(index);
        ListNode node = new ListNode(data);
        cur.prev.next = node;
        node.prev = cur.prev;
        cur.prev = node;
        node.next = cur;
    }

    //找到要插入的点的下标
    public ListNode findIndex(int index){
        ListNode cur = head;
        while(index != 0){
            cur = cur.next;
            index --;
        }
        return cur;
    }

    public int size(){
        ListNode cur = head;
        int cnt = 0;
        while(cur != null){
            cur = cur.next;
            cnt ++;
        }
        return cnt;
    }

    public boolean contain(int key){
        ListNode cur = head;
        while(cur != null){
            if(cur.val == key){
                return true;
            }
            cur = cur.next;
        }
        return false;
    }

    public void remove(int key){
        ListNode cur = head;
        while(cur != null){
            if(cur.val == key){
                if(head == cur){
                    head = head.next;
                    if(head != null) {
                        head.prev = null;
                    }
                } else {
                    cur.prev.next = cur.next;
                    if(cur.next != null){
                        cur.next.prev = cur.prev;
                    } else {
                        last = last.prev;
                    }
                }
                return;
            } else {
                cur = cur.next;
            }
        }
    }

    public void removeAllkey(int key) {
        ListNode cur = head;
        while (cur != null) {
            if (cur.val == key) {
                if (cur == head) {
                    head = head.next;
                    if (head != null) {
                        head.prev = null;
                    }
                } else {
                    cur.prev.next = cur.next;
                    if (cur.next != null) {
                        cur.next.prev = cur.prev;
                    } else {
                        last = last.prev;
                    }
                }
                cur = cur.next;
            } else {
                cur = cur.next;
            }
        }
    }

    public void clear() {
        ListNode cur = head;
        while(cur != null) {
            ListNode curNext = cur.next;
            cur.prev = null;
            cur.next = null;
            cur = curNext;

        }
        head = null;
        last = null;
    }
}
